//
//  KYDoctorViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/17.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYDoctorViewController.h"
#import "KYDoctorInfoModel.h"
#import "KYMatchDorctorModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "Header.h"
#import "KYdoctorReceivingSettingView.h"
#import "KYIntroductionView.h"
#import "UIView+Extension.h"
#import "KYdoctorReceivingSettingView.h"
#import "KYIntroductionView.h"


@interface KYDoctorViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *positionLabel;
@property (weak, nonatomic) IBOutlet UILabel *hosipitalLabel;
@property (weak, nonatomic) IBOutlet UILabel *classLabel;
@property (weak, nonatomic) IBOutlet UILabel *flowerCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *bannerCountLbale;
@property (weak, nonatomic) IBOutlet UILabel *oprationConutLbale;

@property (weak, nonatomic) IBOutlet UIView *buttonView;


@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@property (weak, nonatomic) IBOutlet UIButton *button3;

@property (weak, nonatomic) IBOutlet UIScrollView *infoScrollerView;
@property (nonatomic, weak) KYdoctorReceivingSettingView *SettingView;
@property (nonatomic, weak) KYIntroductionView *introductionView;

@property (nonatomic, strong)KYDoctorInfoModel *doctorInfomodel;

@property (nonatomic, weak)UIImageView *line;
@property (nonatomic, assign)CGFloat lineX;
@end

@implementation KYDoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.button1.selected = YES;
    self.headImage.layer.cornerRadius = self.headImage.bounds.size.height/2;
    self.headImage.layer.masksToBounds = YES;
    [self setDoctorInfoView];
    [self setScrollerView];
    [self setUnderLine];
    self.infoScrollerView.delegate = self;

}
#pragma mark - 设置医生基本信息栏
-(void)setDoctorInfoView{
    __weak typeof(self) weakSelf = self;
    [KYDoctorInfoModel getDoctorInfoModelSuccess:^(KYDoctorInfoModel *doctorInfoModel) {
        weakSelf.doctorInfomodel = doctorInfoModel;
        [weakSelf setdoctorInfoData];
    } withDoctor_id:self.matchDorctorModel.doctor_id];
    
}
-(void)setdoctorInfoData{
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:self.doctorInfomodel.doctor_portrait] placeholderImage:[UIImage imageNamed:@"illness_img_person.png"]];
    self.nameLabel.text = self.doctorInfomodel.doctor_name;
    self.positionLabel.text = self.doctorInfomodel.doctor_title_name;
    self.hosipitalLabel.text = self.doctorInfomodel.hospital_name;
    self.classLabel.text = self.doctorInfomodel.department_name;
    self.flowerCountLabel.text = [NSString stringWithFormat:@"%zd",self.doctorInfomodel.flower];
    self.bannerCountLbale.text = [NSString stringWithFormat:@"%zd",self.doctorInfomodel.banner];
    self.oprationConutLbale.text = [NSString stringWithFormat:@"%zd",self.doctorInfomodel.operation_count];
}
#pragma mark - 设置SrcollerView
-(void)setScrollerView{
    
    CGFloat scrollerViewH = self.infoScrollerView.bounds.size.height;
    self.infoScrollerView.contentSize = CGSizeMake(3*ScreenWith, scrollerViewH);
    //接诊条件
    KYdoctorReceivingSettingView *doctorReceivingSettingView = [[KYdoctorReceivingSettingView alloc]init];
    doctorReceivingSettingView.matchDorctorModel = self.matchDorctorModel;
    doctorReceivingSettingView.frame = CGRectMake(0, 0, ScreenWith, scrollerViewH);
    self.SettingView = doctorReceivingSettingView;
    [self.infoScrollerView addSubview:doctorReceivingSettingView];
    
    //医生简介
    KYIntroductionView *introductionView = [[KYIntroductionView alloc]init];
    introductionView.frame = CGRectMake(ScreenWith, 0, ScreenWith, scrollerViewH);
    introductionView.matchDorctorModel = self.matchDorctorModel;
    [self.infoScrollerView addSubview:introductionView];
    self.introductionView = introductionView;
    //
}
#pragma mark - 三个按钮点击方法
- (IBAction)buttonOneKlick {
    if (self.button1.selected) {
        return;
    }
    [self.infoScrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
    self.lineX = 0;
    [self startAnimation];
    self.button1.selected = YES;
    self.button2.selected = NO;
    self.button3.selected = NO;
    
}
- (IBAction)buttonTwoKlick:(id)sender {
    if (self.button2.selected) {
        return;
    }
    [self.introductionView refreshData];
    [self.infoScrollerView setContentOffset:CGPointMake(ScreenWith, 0) animated:YES];
    self.lineX = self.button2.frame.origin.x;
    [self startAnimation];
    self.button2.selected = YES;
    self.button1.selected = NO;
    self.button3.selected = NO;
}
- (IBAction)buttonThreeKlick {
    if (self.button3.selected) {
        return;
    }
    [self.infoScrollerView setContentOffset:CGPointMake(2*ScreenWith, 0) animated:YES];
    self.lineX = self.button3.frame.origin.x;
    [self startAnimation];
    self.button3.selected = YES;
    self.button1.selected = NO;
    self.button2.selected = NO;
}
-(void)setUnderLine{
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.button1.frame), ScreenWith/3, 3)];
    line.image = [UIImage imageNamed:@"link_button_01"];
    self.line = line;
    [self.buttonView addSubview:line];
}
-(void)startAnimation{
    [UIView animateWithDuration:0.3 animations:^{
        self.line.x = self.lineX;
    }];
}
#pragma  mark -scrollView的代理方法
//减速停下来的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.infoScrollerView.contentOffset.x == 0) {
        [self buttonOneKlick];
    }
    if (self.infoScrollerView.contentOffset.x == ScreenWith) {
        [self buttonTwoKlick:nil];
    }
    if (self.infoScrollerView.contentOffset.x == ScreenWith*2) {
        [self buttonThreeKlick];
    }
}

@end
